package com.boot.controller;

import com.boot.entity.Term;
import com.boot.repository.TermRepository;
import com.boot.vo.BaseMessage;
import com.boot.vo.MessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/term")
public class TermController {

    @Autowired
    private TermRepository termRepository;

    @GetMapping("list")
    private BaseMessage<?> list() {
        return MessageHandler.createSuccessVo(termRepository.findAll(), "success");
    }


    @PostMapping
    public BaseMessage<?> addOrUpdate(@RequestBody Term term) {
        termRepository.save(term);
        return MessageHandler.createSuccessVo(term.getTermid() != null ? "修改成功！" : "保存成功！");
    }

    @DeleteMapping("/{id}")
    public BaseMessage<?> del(@PathVariable Integer id) {
        termRepository.deleteById(id);
        return MessageHandler.createSuccessVo("删除成功！");
    }

}
